<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>link</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 07/03/2006</div>
    <p>
      <b>link</b> -  incremental linker </p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>x=link(files [, sub-names,flag]);  </tt>
      </dd>
      <dd>
        <tt>link(x , sub-names [, flag]);     </tt>
      </dd>
      <dd>
        <tt>ulink(x)  </tt>
      </dd>
      <dd>
        <tt>lst=link('show')  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>files</b>
        </tt>: a character string or a vector of character strings, the files
	    names used to define the new entry point (compiled routines, user
	    libraries, system libraries,..)</li>
      <li>
        <tt>
          <b>sub-names</b>
        </tt>: a character string or a vector of character strings . Name of
	    the entry points in <tt>
          <b>files</b>
        </tt> to be linked.</li>
      <li>
        <tt>
          <b>x</b>
        </tt>: an integer which gives the id of a shared library linked into
	    Scilab with a previous call to <tt>
          <b>link</b>
        </tt>.</li>
      <li>
        <tt>
          <b>flag</b>
        </tt>: character string <tt>
          <b>'f'</b>
        </tt> or <tt>
          <b>'c'</b>
        </tt> for
	    Fortran (default) or C code.</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      <tt>
        <b>link</b>
      </tt> is a incremental link facility based on the dld
      library: this command allows to add new compiled 
      Fortran or C routines to Scilab executable code.
      Linked routines can be called interactively by the function <tt>
        <b>fort</b>
      </tt>.
      Linked routines can also be used as "external" for e.g. 
      non linear problem solvers (<tt>
        <b>ode</b>
      </tt>, <tt>
        <b>optim</b>
      </tt>, <tt>
        <b>intg</b>
      </tt>, 
      <tt>
        <b>dassl</b>
      </tt>...). 
    </p>
    <p>a call to <tt>
        <b>link</b>
      </tt> returns an integer which gives the
	  id of the shared  library which is loaded into Scilab. This number
	  can then be used as the first argument of the link function in order
	  to link additional  function from the linked shared library. The
	  shared library is  removed with the <tt>
        <b>ulink</b>
      </tt> command.</p>
    <p>A routine  can be unlinked with
      <tt>
        <b>ulink</b>
      </tt>.  If the linked function has been
      modified between two links, it is required to <tt>
        <b>ulink</b>
      </tt> the previous instance
      before the new link.</p>
    <p>
      <tt>
        <b>link('show')</b>
      </tt> returns the current linked routines.</p>
    <p> To be able to link routines in a system independent way, it is convenient
      to use the <tt>
        <b>ilib_for_link</b>
      </tt> utility function instead of <tt>
        <b>link</b>
      </tt>.</p>
    <p>(Experienced) users may also <tt>
        <b>link</b>
      </tt> a new Scilab interface routine 
      to add a set of new functions. See <a href="intersci.htm">
        <tt>
          <b>intersci</b>
        </tt>
      </a> documentation
      for interface generation and <a href="ilib_build.htm">
        <tt>
          <b>ilib_build</b>
        </tt>
      </a> and
      <a href="../functions/addinter.htm">
        <tt>
          <b>addinter</b>
        </tt>
      </a> functions.  </p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>
    //Example of the use of ilib_for_link with  a simple C code
    f1=['#include &lt;math.h&gt;'
    'void fooc(c,a,b,m,n)'
    'double a[],*b,c[];'
    'int *m,*n;'
    '{'
    '   int i;'
    '   for ( i =0 ; i &lt; (*m)*(*n) ; i++) '
    '     c[i] = sin(a[i]) + *b; '
    '}'];

    mputl(f1,'fooc.c')

    //creating the shared library: a Makefile and a loader are 
    //generated, the code is compiled and a shared library built.
    ilib_for_link('fooc','fooc.o',[],"c") 

    // display the loader.sce file which calls link
    mprintf('%s\n',mgetl('loader.sce')) 
    // load the shared library 
    exec loader.sce 

    link('show') 
    // call the new linked entry point
    a=linspace(0,%pi,10);b=5;
    y1=call('fooc',a,2,'d',b,3,'d',size(a,1),4,'i',size(a,2),5,'i','out',size(a),1,'d')
    // check
    y1-(sin(a)+b)
    
    </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="../programming/call.htm">
        <tt>
          <b>call</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../programming/external.htm">
        <tt>
          <b>external</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="c_link.htm">
        <tt>
          <b>c_link</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../functions/addinter.htm">
        <tt>
          <b>addinter</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="ilib_for_link.htm">
        <tt>
          <b>ilib_for_link</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="ilib_build.htm">
        <tt>
          <b>ilib_build</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
